#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "sensor.h"

SensorControl ctrl = {
    .auto_light = 0,   // 自动灯光
    .fresh_fan = 0,    // 自动模式
    .breath_led = 4,   // 慢速呼吸灯
    .seat_ctrl = 0,    // 默认座椅位置
    .wiper_ctrl = 0,
};

SensorData data = {
    .light_level = 3600,   // 自动灯光
    .temperature = 26.0,    // 自动模式
    .humidity = 70.0,   // 慢速呼吸灯
    .rain_level = 0.22,    // 默认座椅位置
};

void data_set_light(int light) {
    data.light_level=light;
}

void data_set_th(float temperature,float humidity) {
    data.temperature=temperature;
    data.humidity=humidity;
}

void data_set_rain(float rain) {
    data.rain_level=rain;
}

void data_to_string(char* buffer,size_t size,SensorData* temp){
    snprintf(buffer,size,"sensor data:[%d,%.2f,%.2f,%.2f]",
            temp->light_level,
            temp->temperature,
            temp->humidity,
            temp->rain_level);
}

SensorData data_get(void) {
    return data;
}

void sensor_change(char* data) {
    printf("d0:%d,d1:%d,d2:%d,d3:%d,d4:%d\n",data[0],data[1],data[2],data[3],data[4]);
    ctrl.auto_light = data[0] - '0';
    ctrl.fresh_fan = data[1] - '0';
    ctrl.breath_led = data[2] - '0';
    ctrl.seat_ctrl = data[3] - '0';
    ctrl.wiper_ctrl = data[4] - '0';
}

SensorControl sensor_get(void) {
    return ctrl;
}